//步骤 1
//创建一个命令接口。
public class Order {
   virtual void execute()=0;
}
//步骤 2
//创建一个请求类。
public class Stock {
   private:
   String name = "ABC";
   int quantity = 10;
 
   public:
   void buy(){
      System.out.println("Stock [ Name: "+name+", 
         Quantity: " + quantity +" ] bought");
   }
   void sell(){
      System.out.println("Stock [ Name: "+name+", 
         Quantity: " + quantity +" ] sold");
   }
}
// 步骤 3
// 创建实现了 Order 接口的实体类。
public class BuyStock :public Order {
   private:
   Stock abcStock;
 
   public:
   BuyStock(Stock abcStock){
      this.abcStock = abcStock;
   }
   void execute() {
      abcStock.buy();
   }
}
public class SellStock :public Order {
   private Stock abcStock;
 
   public SellStock(Stock abcStock){
      this.abcStock = abcStock;
   }
 
   public void execute() {
      abcStock.sell();
   }
}
// 步骤 4
// 创建命令调用类。
public class Broker {
   private:
   list<Order> orderList = new list<Order>(); 
 
   public:
   void takeOrder(Order order){
      orderList.push_back(order);      
   }
 
   void placeOrders(){
      for (auto &order : orderList) {
         order.execute();
      }
      orderList.clear();
   }
}
// 步骤 5
// 使用 Broker 类来接受并执行命令。
int main() 
{
      Stock abcStock = new Stock();
 
      BuyStock buyStockOrder = new BuyStock(abcStock);
      SellStock sellStockOrder = new SellStock(abcStock);
 
      Broker broker = new Broker();
      broker.takeOrder(buyStockOrder);
      broker.takeOrder(sellStockOrder);
 
      broker.placeOrders();
   }
}